package com.gitee.huxing.algorithmic;

import java.util.Arrays;

/**
 * @Description: 位预算
 * @ProjectName: AlgorithmicPactice
 * @Package: com.gitee.huxing.algorithmic
 * @ClassName: BitCalculate
 * @Author: huxing
 * @DateTime: 2022-02-28 9:44 AM
 */
public class BitCalculate {

    public static void main(String[] args) {
//        int n = 5;
//        int[][] requests = {{0,1},{1,0},{0,1},{1,2},{2,0},{3,4}};
//        maximumRequests(n, requests);
        int[] nums = new int[]{1,2,3};
        System.out.println(subArrayRanges(nums));
    }

    public static int maximumRequests(int n, int[][] requests) {
        int[] detail = new int[n];
        int ans = 0, m = requests.length;
        for(int mask = 0; mask < (1 << m); ++mask){
            int count = Integer.bitCount(mask);
            if (count <= ans){
                continue;
            }
            Arrays.fill(detail, 0);
            for(int i = 0; i < m; ++i){
                if ((mask & (1 << i)) != 0){
                    ++detail[requests[i][0]];
                    --detail[requests[i][1]];
                }
            }
            boolean flag = true;
            for (int x : detail){
                if ( x != 0){
                    flag = false;
                    break;
                }
            }
            if (flag){
                ans = count;
            }
        }
        return ans;
    }

    public static long subArrayRanges(int[] nums) {
        long total = 0;
        for(int i = 0 ; i < nums.length; i++){
            int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
            for(int j = i; j< nums.length; j++){
                max = Math.max(max, nums[j]);
                min = Math.min(min, nums[j]);
                total += max - min;
            }
        }
        return total;
    }
}
